/**
 * Copyright 2005 Darren L. Spurgeon
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ce.cetag.tags.tabs;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

/**
 * Tag handler for AJAX tabbed panel.
 *
 * @author Darren Spurgeon
 * @version $Revision: 1.1 $ $Date: 2008/01/28 06:47:33 $ $Author: Code1_xuhaidui $
 */
public class AjaxTabPanelTag extends TagSupport {





	@Override
	public int doStartTag() throws JspException {

		this.panelId = (String) ExpressionEvaluatorManager.evaluate(
				"panelStyleId", this.panelId, String.class, this,
				super.pageContext);
		if(this.hasTop==null || this.hasTop.equals("")){
			this.hasTop="true";
		}

		COUNT_TABS=0;
		JS=new StringBuffer("");

		StringBuffer script = new StringBuffer();
		script.append("<script type=\"text/javascript\">\n");
		script.append("var "+this.panelId+"oldID=\"\";\n");
		script.append("var "+this.panelId+"_oldtab_top=\"\";\n");
		script.append("var "+this.panelId+"_oldtab_right=\"\";\n");
		script.append("function "+this.panelId+"showDiv(tag_top,tag_right,url,tabHeight,jsParam){");
		script.append("if("+this.panelId+"_oldtab_top!=\"\")");
		script.append("parentDiv_tag_top_"+this.panelId+".appendChild(document.getElementById("+this.panelId+"_oldtab_top));");
		script.append("if(tag_top!=\"\"){");
		script.append("tag_top_"+this.panelId+".appendChild(document.getElementById(tag_top));");
		script.append("document.getElementById(tag_top).style.display= \"\";}");
		script.append(""+this.panelId+"_oldtab_top=tag_top;");
		script.append("if("+this.panelId+"_oldtab_right!=\"\")");
		script.append("parentDiv_tag_right_"+this.panelId+".appendChild(document.getElementById("+this.panelId+"_oldtab_right));");
		script.append("if(tag_right!=\"\"){");
		script.append("tag_right_"+this.panelId+".appendChild(document.getElementById(tag_right));");
		script.append("document.getElementById(tag_right).style.display= \"\";}");
		script.append(""+this.panelId+"_oldtab_right=tag_right;");

		script.append("if(jsParam!=\"\"){");
		script.append("var tempjsParam=jsParam.split(\",\");");
		script.append("for(var i=0;i<tempjsParam.length;i++) {");
		script.append("var tempObjc=eval(tempjsParam[i]);");
		script.append("url=url.replace(\":\"+tempjsParam[i],tempObjc);");

		script.append("}}");

		if(!("".equals(this.iframeId))) {
			script.append("document.getElementById(\""+this.iframeId+"\").src=url;");
		} else {
			script.append("document.getElementById(\"con_"+this.panelId+"\").src=url;");
		}

		script.append("if(tabHeight!=\"\"){");

		if(!("".equals(this.iframeId))) {

			script.append("document.getElementById(\""+this.iframeId+"\").height=tabHeight;");

		} else {

			script.append("document.getElementById(\"con_"+this.panelId+"\").height=tabHeight;");

		}
		script.append("} else {");
		script.append("if('"+this.defaultHeight+"'!=\"\"){");
		if(!("".equals(this.iframeId))) {

			script.append("document.getElementById(\""+this.iframeId+"\").style.height='"+this.defaultHeight+"';");

		} else {

			script.append("document.getElementById(\"con_"+this.panelId+"\").style.height='"+this.defaultHeight+"';");

		}
		script.append("}}");


		script.append("}");
		script.append("</script>\n");
		script.append("<div class=\"container\">");
		script.append("<div id=\"parentDiv_tag_top_"+this.panelId+"\" style=\"display:none\"></div>");
		script.append("<div id=\"parentDiv_tag_right_"+this.panelId+"\" style=\"display:none\"></div>");
		script.append("<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
		script.append("<tr>");
		if((this.hasTop.trim()).equals("false")){
			script.append("<td>");
		}
		else{
			script.append("<td class=\"bg_search\">");
		}

		script.append("<div id=\"tag_top_"+this.panelId+"\">");
		script.append("<div>");
		script.append("</td>");
		script.append("</tr>");
		script.append("<tr>");
		script.append("<td class=\"bg_toolBar2\">");
		script.append("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
		script.append("<tr>");
		script.append("<td width=\""+this.barWidthLeft+"\">");
		script.append("<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
		script.append("<tr>");

		JspWriter writer = pageContext.getOut();
		try {
			writer.println(script);
		} catch (IOException e) {
			throw new JspException(e.getMessage());
		}
		return EVAL_BODY_INCLUDE;
	}

	@Override
	public int doEndTag() throws JspException {


		StringBuffer script = new StringBuffer();

		script.append("</tr></table></td>");
		script.append("<td width=\""+this.barWidthRight+"\" align=\"right\">");
		script.append("<div id=\"tag_right_"+this.panelId+"\">");
		script.append("</div>");
		script.append("</td></tr></table></td><tr>");
		script.append("<td valign=\"top\">");
		script.append("<div id=\"tag_bottom_"+this.panelId+"\" width=\"100%\" height=\"100%\">");

		if(!("".equals(this.iframeId))) {

			script.append("<iframe name=\""+this.iframeId+"\" src=\"#\" class=\"bg_iframe2\" MARGINWIDTH=\"0\" MARGINHEIGHT=\"0\" SCROLLING=\"no\" frameborder=\"0\"></iframe>");
		} else {

			script.append("<iframe name=\"con_"+this.panelId+"\" src=\"#\" class=\"bg_iframe2\" MARGINWIDTH=\"0\" MARGINHEIGHT=\"0\" SCROLLING=\"no\" frameborder=\"0\"></iframe>");
		}

		script.append("</div></td></tr></table>");
		script.append("</div>");

		script.append(getDefaultTabStr());

		script.append("<script type=\"text/javascript\">\n");

		script.append("newTop_ID_1=defaultTabOldTop_ID_1;");
		script.append("newRight_ID_2=defaultTabOldRight_ID_2;");
		script.append("newBody_ID_3=defaultTabOldBody_ID_3;");
		script.append("newBody_tab_height=defaultTabOldBody_height;");
		script.append("newBody_tab_jsParam=defaultTabOldBody_jsParam;");
		script.append(AjaxTabPanelTag.getPanelID()+"showDiv(newTop_ID_1,newRight_ID_2,newBody_ID_3,newBody_tab_height,newBody_tab_jsParam);");
		script.append("</script>\n");



		JspWriter writer = pageContext.getOut();
		try {
			writer.println(script);
			script=null;
		} catch (IOException e) {
			throw new JspException(e.getMessage());
		}

		return EVAL_PAGE;
	}
	private static StringBuffer JS=new StringBuffer("");

	private static Integer COUNT_TABS=0;

	private static String PANEL_ID;

	private String  panelId;

	private String hasTop;

	private String iframeId="";

	private String defaultHeight="";

	private String barWidthRight="35%";

	private String barWidthLeft="65%";


	public String getDefaultHeight() {
		return defaultHeight;
	}

	public void setDefaultHeight(String defaultHeight) {
		this.defaultHeight = defaultHeight;
	}

	public String getIframeId() {
		return iframeId;
	}

	public void setIframeId(String iframeId) {
		this.iframeId = iframeId;
	}

	public String getPanelId() {
		return panelId;
	}

	public void setPanelId(String panelId) {
		this.PANEL_ID = panelId;
		this.panelId = panelId;
	}



	public void setHasTop(String hasTop){
		this.hasTop=hasTop;
	}

	public String getHasTop(){
		return hasTop;
	}

	public static String getPanelID(){
		return PANEL_ID;
	}

	public static Integer getCountTabs(){
		return ++COUNT_TABS;
	}

	public static void setDefaultTabStr(final StringBuffer js){
		JS=js;
	}

	public StringBuffer getDefaultTabStr() {
		return JS;
	}

	public String getBarWidthLeft() {
		return barWidthLeft;
	}

	public void setBarWidthLeft(String barWidthLeft) {
		this.barWidthLeft = barWidthLeft;
	}

	public String getBarWidthRight() {
		return barWidthRight;
	}

	public void setBarWidthRight(String barWidthRight) {
		this.barWidthRight = barWidthRight;
	}

	@Override
	public void release() {
		this.panelId = null;
		this.hasTop = null;
		this.iframeId = null;
		this.barWidthRight = null;
		this.barWidthLeft = null;
		super.release();
	}



}
